<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>累加数</title>
</head>
<body>
  <script>
    function fn(num) {
      // a,  b代表前两个数的长度
      function helper(a, b) {
        let num_a = Number(num.substr(0, a))
        let num_b = Number(num.substr(a, b))
        // 确保最少3个数，利用str转数字会去掉前置0判断数不能是0开头的（除0本身外）
        if (num.length <= a + b || num_a.toString() != num.substr(0, a) || num_b.toString() != num.substr(a, b)) return false
        // 循环判断是否是累加序列（字符串匹配）
        let str = num.substr(a + b)
        while (str) {
          let sum = num_a + num_b;
          if (str.substr(0, sum.toString().length) == sum.toString()) {
            [str, num_a, num_b] = [str.substr(sum.toString().length), num_b, sum]
          } else {
            return false
          }
        }
        return true
      }
      // 2个循环确定开头两个数字，开头两个数字确定了那么序列就是唯一的
      for (let i = 1; i < num.length; i += 1) {
        for (let j = 1; j < num.length; j += 1) {
          if (helper(i, j)) return true
        }
      }
      return false;
    }
    console.log(fn('112358'))
  </script>
</body>
</html>